//
//  RecordPlayView.m
//  视频播放
//
//  Created by user1 on 2018/8/3.
//  Copyright © 2018年 Beijing Youjiu Network. All rights reserved.
//

#import "RecordPlayView.h"

@interface RecordPlayView()


@property (nonatomic ,strong)UILabel *title;

@property (nonatomic ,strong)UILabel *timeLabel;

@end

@implementation RecordPlayView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction2:)];
        [self addGestureRecognizer:tap2];
        self.backgroundColor = UIColorFromRGB(0xFFFFFF);
        [self initView];
        [self layout];
        [self addNotification];
    }
    return self;
}

- (void)initView{
    [self addSubview:self.playBtn];
    [self addSubview:self.title];
    [self addSubview:self.timeLabel];
}

- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeStatus:) name:@"changeStatus2" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioPlayEnd:) name:@"audioEnd" object:nil];
}

- (void)audioPlayEnd:(NSNotification *)notif{
    if (self.playBtn.selected) {
        self.playBtn.selected = !self.playBtn.selected;
    }
}

- (void)changeStatus:(NSNotification *)notif{
    NSDictionary *dic = notif.userInfo;
    BOOL status = [dic[@"status"] boolValue];
    BOOL isPlay = [dic[@"isPlay"] boolValue];
    if (status == YES) {
        if (status == self.playBtn.selected) {
            self.playBtn.selected = !status;
        }
    }else{
        if (!isPlay) {
            self.playBtn.selected = !status;
        }
    }
}

- (void)tapAction2:(UITapGestureRecognizer *)tap{
    self.playBtn.selected = !self.playBtn.selected;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeStatus" object:nil userInfo:@{@"status":@(self.playBtn.selected),
                                                                                                     @"isPlay":@(self.isPlay)
                                                                                                     }];
    if (self.clickRecordBlock) {
        self.clickRecordBlock(self.playBtn.selected);
    }
}

- (void)showRecordView {
    [UIView animateWithDuration:1.0 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.y = Fix375(480) + Fix375(50);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hideRecordView {
    [UIView animateWithDuration:0.0 animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
        self.alpha = 0.f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setTitleString:(NSString *)titleString{
    _titleString = titleString;
    self.title.text = titleString;
}

- (void)setTime:(float)time{
    _time = time;
    NSString *timeString = [NSString stringWithFormat:@"%f",time];
    NSString *string1;
    NSString *string2;
    if (time < 60) {
        string1 = [timeString substringWithRange:NSMakeRange(0, 1)];
        if ([string1 floatValue] < 10) {
            string1 = [NSString stringWithFormat:@"0%@",string1];
        }
        self.timeLabel.text = [NSString stringWithFormat:@"00:%@",string1];
    }else{
        time = time - 60;
        int minute = (int)time;
        if (minute < 10) {
            string2 = [NSString stringWithFormat:@"0%d",minute];
        }
        NSString *string = [NSString stringWithFormat:@"%2f",time];
        string1 = [string substringWithRange:NSMakeRange(string.length - 2, 1)];
        if ([string1 floatValue] < 10) {
            string1 = [NSString stringWithFormat:@"0%@",string1];
        }
        self.timeLabel.text = [NSString stringWithFormat:@"%@:%@",string2,string1];
    }
}


- (void)playClick:(UIButton *)btn{
    self.playBtn.selected = !self.playBtn.selected;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeStatus" object:nil userInfo:@{@"status":@(self.playBtn.selected),
                                                                                                     @"isPlay":@(self.isPlay)
                                                                                                     }];
    if (self.clickRecordBlock) {
        self.clickRecordBlock(self.playBtn.selected);
    }
}

- (void)layout{
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Fix375(15));
        make.top.mas_equalTo(Fix375(11));
        make.size.mas_equalTo(CGSizeMake(Fix375(29), Fix375(29)));
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Fix375(59));
        make.top.mas_equalTo(Fix375(14));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Fix375(15));
        make.top.mas_equalTo(Fix375(14));
    }];
}

-(UIButton *)playBtn {
    if (_playBtn == nil) {
        _playBtn = [[UIButton alloc]init];
        [_playBtn addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

- (UILabel *)title{
    if (!_title) {
        _title = [[UILabel alloc]init];
        _title.textColor = UIColorFromRGB(0x333333);
        _title.font = RMFont(16);
    }
    return _title;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.textColor = UIColorFromRGB(0xD53E4A);
        _timeLabel.font = RMFont(16);
    }
    return _timeLabel;
}

@end
